<template>
  <el-table
    :data="tableData"
    style="width: 100%"
    height="calc(100% - 100px)"
    size="large"
    highlight-current-row
  >
    <el-table-column label="评论编号" prop="id" align="center" width="100px" />
    <el-table-column label="用户头像" sortable width="120px" align="center">
      <template #default="scope">
        <img :src="scope.row.avatarUrl" />
      </template>
    </el-table-column>
    <el-table-column label="用户昵称" prop="nickName" align="center" show-overflow-tooltip />
    <el-table-column label="评论内容" prop="info" sortable align="center" show-overflow-tooltip />
    <el-table-column label="回复内容" sortable align="center" show-overflow-tooltip>
      <template #default="scope">
        <div v-if="scope.row.reply">{{ scope.row.reply }}</div>
        <el-tag v-else style="margin: 0 5px" type="warning">等待回复</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="是否置顶" sortable width="120px" align="center">
      <template #default="scope">
        <el-tag v-if="scope.row.isTop" style="margin: 0 5px">置顶</el-tag>
        <el-tag v-else style="margin: 0 5px">不置顶</el-tag>
      </template>
    </el-table-column>
    <el-table-column label="发布时间" prop="createTime" sortable width="150px" align="center">
      <template #default="scope">{{ scope.row.createTime.substring(0, 10) }}</template>
    </el-table-column>
    <el-table-column align="right" width="250px">
      <template #header>
        <el-input v-model="search" size="default" placeholder="Type to search" />
      </template>
      <template #default="scope">
        <el-button size="small" @click="handleEdit(scope.row)" type="primary">编辑</el-button>
        <el-popconfirm title="Are you sure to delete this?" @confirm="handleDelete(scope.row)">
          <template #reference>
            <el-button size="small" type="danger">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="next">
    <el-pagination
      background
      layout="prev, pager, next"
      :total="count"
      @current-change="handleCurrentChange"
    />
  </div>

  <!-- 弹出层 -->
  <el-dialog v-model="isShow" title="评论回复" width="30%" align-center>
    <el-form :model="currentComment" label-width="120px" label-position="top">
      <el-form-item label="回复内容">
        <el-input
          v-model="currentComment.reply"
          :autosize="{ minRows: 2, maxRows: 4 }"
          maxlength="200"
          show-word-limit
          type="textarea"
          placeholder="请输入回复内容... "
        />
      </el-form-item>
      <el-form-item label="是否置顶评论">
        <el-radio-group v-model="currentComment.isTop">
          <el-radio :label="1">置顶</el-radio>
          <el-radio :label="0">不置顶</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="reply"> 确认 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import axios from '@/utils/request'
import 'animate.css'
import { onMounted, reactive, ref } from 'vue'
let tableData = ref([])
const search = ''
let isShow = ref(false)
let count = ref(10)
let commentId = ref<number>()
const init = async () => {
  let countData = await axios.get('/comment/count')
  count.value = countData.data
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/comment',
    params: {
      page: 1,
      take: 10,
    },
  })
  tableData.value = result.data
}
let currentComment = reactive({
  reply: '',
  isTop: 0,
})
onMounted(() => {
  init()
})

const handleCurrentChange = async (page: any) => {
  const result = await axios({
    method: 'get',
    url: 'https://www.tinyxu.fun/v1/comment',
    params: {
      page,
      take: 10,
    },
  })
  tableData.value = result.data
}
// 编辑
const handleEdit = (row: any) => {
  isShow.value = true
  commentId.value = row.id
  currentComment.isTop = row.isTop
  currentComment.reply = row.reply
}

const reply = async () => {
  try {
    await axios.patch(`/comment/${commentId.value}`, {
      ...currentComment,
    })
    ElMessage.success('更新成功')
    isShow.value = false
    init()
  } catch (error) {
    ElMessage.error('更新失败')
  }
}

// 删除操作
const handleDelete = async (row: any) => {
  try {
    await axios.delete(`/comment/${row.id}`)
    ElMessage.success('删除成功')
    init()
  } catch (error) {
    ElMessage.error('出错了!删除失败')
  }
}
</script>
<style lang="less" scoped>
img {
  width: 45px;
  height: 45px;
  object-fit: contain;
  border-radius: 4px;
}

.next {
  height: 80px;
  display: flex;
  justify-content: flex-end;
}
</style>
